import request from '@/utils/request'
import Restfull from '@/api/restfull-api'

const USER_BASE_URL = '/user'

export const userRestfull = new Restfull(USER_BASE_URL)

export function login(data) {
  const params = new FormData()
  for (const i in data) {
    if (Object.prototype.hasOwnProperty.call(data, i)) {
      params.append(i, data[i])
    }
  }
  return request({
    url: '/login',
    method: 'post',
    headers: { 'Content-Type': 'multipart/form-data' },
    data: params
  })
}

export function getInfo() {
  return request({
    url: `${USER_BASE_URL}/current`,
    method: 'get'
  })
}

export function logout() {
  return request({
    url: `/logout`,
    method: 'get'
  })
}

export function changeUserStatus({ id, active }) {
  return request({
    url: `${USER_BASE_URL}/changeUserStatus`,
    method: 'put',
    data: {
      id,
      active
    }
  })
}

export function resetPassword({ id, password }) {
  return request({
    url: `${USER_BASE_URL}/resetPassword`,
    method: 'put',
    data: {
      id,
      password
    }
  })
}
